package com.yoomiito.app.ui.my.angle

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import android.text.TextUtils
import android.view.View
import chihane.jdaddressselector.BottomDialog
import cn.droidlover.xdroidmvp.cache.SharedPref
import com.yoomiito.app.Constant
import com.yoomiito.app.R
import com.yoomiito.app.base.App
import com.yoomiito.app.base.BaseActivity
import com.yoomiito.app.model.my.AngleUserInfo
import com.yoomiito.app.model.my.AngleUserInfoData
import com.yoomiito.app.utils.AllCode
import com.yoomiito.app.utils.EventMessage
import com.yoomiito.app.utils.YMToast
import com.yoomiito.app.widget.dialog.TipDialog
import kotlinx.android.synthetic.main.act_angle_user_info.*
import kotlinx.android.synthetic.main.title_bar.*

class AngleUserInfoActivity : BaseActivity<AngleUserInfoPresenter>() {
    companion object {
        fun start(context: Context) {
            val intent = Intent(context, AngleUserInfoActivity::class.java)
            context.startActivity(intent)
        }
    }

    private val mAdapter = AngleUserInfoAdapter(null)
    private var userInfo: AngleUserInfo? = null
    private lateinit var name: String
    private lateinit var mobile: String
    private lateinit var identity: String
    private lateinit var areaName: String
    private lateinit var address: String
    private lateinit var mailbox: String
    private var mCompleteIfo:Boolean = false
    override fun getLayoutId(): Int = R.layout.act_angle_user_info

    override fun newP(): AngleUserInfoPresenter = AngleUserInfoPresenter(App.mAppConfig)

    override fun initData(savedInstanceState: Bundle?) {
        tv_center.text = "个人信息"
        tv_center.paint.isFakeBoldText = true
        tv_right.text = "下一步"
        tv_right.visibility = View.VISIBLE
        tv_right.setOnClickListener {
            if (userInfo!=null&&!userInfo?.name.isNullOrEmpty()||mCompleteIfo){//如果已经填写 则直接跳转
                uploadResult()
                return@setOnClickListener
            }
            for ((i, it) in mAdapter.data.withIndex()) {
//                if (it.value.isNullOrEmpty()) {
//                    YMToast.showText("请完善个人信息")
//                    return@setOnClickListener
//                }
                when (i) {
                    0 -> name = it.value
                    1 -> mobile = it.value
                    2 -> identity = it.value
                    3 -> areaName = it.value
                    4 -> address = it.value
                    5 -> mailbox = it.value
                }
            }
            showTip()
        }
        iv_back_left.setOnClickListener { finish() }

        rcy.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(this)
        rcy.adapter = mAdapter
        showLoading()
        p.getInfo()
    }

    fun setData(t: AngleUserInfo?) {
        dismissLoading()
        userInfo = t
        val data = mutableListOf<AngleUserInfoData>()
        data.add(AngleUserInfoData("姓名",
                if (t == null || t.name.isNullOrEmpty()) "" else t.name,
                t == null || t.name.isNullOrEmpty(),false))
        data.add(AngleUserInfoData("联系电话", if (t == null || t.mobile.isNullOrEmpty()) "" else t.mobile,t == null || t.mobile.isNullOrEmpty(), false))
        data.add(AngleUserInfoData("身份证号", if (t == null || t.identity.isNullOrEmpty()) "" else t.identity, t == null || t.identity.isNullOrEmpty(),false))
        data.add(AngleUserInfoData("所在地区", if (t == null || t.areaName.isNullOrEmpty()) "" else t.areaName, t == null || t.areaName.isNullOrEmpty(),t == null || t.areaName.isNullOrEmpty()))
        data.add(AngleUserInfoData("详细地址", if (t == null || t.address.isNullOrEmpty()) "" else t.address, t == null || t.address.isNullOrEmpty(),false))
        data.add(AngleUserInfoData("邮箱信息", if (t == null || t.mailbox.isNullOrEmpty()) "" else t.mailbox, t == null || t.mailbox.isNullOrEmpty(),false))
        mAdapter.setNewData(data)
        if (mCompleteIfo){SignAgreementActivity.start(this)}
    }

    private fun showTip() {
        val tipDialog = TipDialog(this)
        tipDialog.setSureText("立即提交")
                .setCancelText("取消")
                .setTitle("温馨提示")
                .setContent("请确保所有信息必须按真实信息填写，一旦提交不可更改")
                .setClickListener {
                    tipDialog.dismiss()
                    showLoading()
                    p.upload(name,
                            mobile,
                            identity,
                            areaName,
                            address,
                            mailbox)
                }
                .show()
    }

    fun uploadResult() {
        mCompleteIfo = true
        showLoading()
        p.getInfo()
    }

    override fun eventMessage(eventMessage: EventMessage?) {
        super.eventMessage(eventMessage)
        if (AllCode.ANGLE_AGREEMENT_SIGN == eventMessage?.eventType){
            finish()
        }
    }
}